<?php
/**
 * Created by PhpStorm.
 * User: gaoweihua
 * Date: 2019/2/17
 * Time: 4:21 PM
 */
$func = function($msg){
    $fileName = dirname(__FILE__) . '/log.txt';
    $contents = is_file($fileName) ? file_get_contents($fileName) : '';
    file_put_contents($fileName,$contents . $msg);
};
$serv = new swoole_server('127.0.0.1', 9526, SWOOLE_BASE);

$serv->set(array(
    'worker_num' => 1,
    'task_worker_num' => 4,
    'daemonize'=>1,
));
$serv->on('Start',function($server) use($func){
    echo '服务已开启' . PHP_EOL;
    $func('服务已开启' . PHP_EOL);

});

$serv->on('Receive',function(swoole_server $serv, $fd, $from_id, $data) use ($func) {
    echo '接收数据，数据长度：' . strlen($data) . PHP_EOL;
    $serv->task($data,0);
    $func('接收数据' . PHP_EOL);
});

$serv->on('Task', function (swoole_server $serv, $task_id, $from_id, $data) use ($func) {
    echo "Task进程开始工作" . PHP_EOL;
    //echo "#{$serv->worker_id}\tonTask: [PID={$serv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".PHP_EOL;
    $func('Task进程开始工作' . PHP_EOL);
    $serv->finish($data);
});

$serv->on('Finish', function(swoole_server $serv, $task_id, $data) use ($func) {
    echo 'task任务处理完成' . PHP_EOL;
    $func('Task任务处理完成' . PHP_EOL);
});

$serv->start();

